********************************************************************************
*** WIOD 2013 release
********************************************************************************
clear
set more off

*global rootfolder ""
global wiodfolder "$rootfolder\data\3_WIOD"
cd "$wiodfolder"

set mem 700m
set matsize 6500
set maxvar 32767

********************************************************************************
***LOAD DATA TO STATA
********************************************************************************
global WEB 0 // set to 0: to use wiod files used in the paper, 1: to download files directly from the web


foreach yy in 95 96 97 98 99 00 01 02 03 04 05 06 07 08 09 10 11 {
	if $WEB == 0 {
		if `yy' == 95 | `yy' == 96 | `yy' == 97 | `yy' == 98 | `yy' == 99 {
			local i = "19"+"`yy'"
		}
		
		if `yy' == 00 | `yy' == 01 | `yy' == 02 | `yy' == 03 | `yy' == 04 | `yy' == 05 | `yy' == 06 | `yy' == 07 | `yy' == 08 | `yy' == 09 | `yy' == 10 | `yy' == 11 {
			local i = "20"+"`yy'"
		}
	insheet using "$wiodfolder\Intercountry Input-Output Table for `i'.csv", clear comma names
	}
	
	if $WEB == 1 {
		if `yy' == 95 | `yy' == 96 | `yy' == 97 | `yy' == 98 | `yy' == 99 {
			local i = "19"+"`yy'"
			import excel using "http://www.wiod.org/protected3/data13/wiot_analytic/wiot`yy'_row_apr12.xlsx",clear
			}
			
		if `yy' == 00 | `yy' == 01 | `yy' == 02 | `yy' == 03 | `yy' == 04 | `yy' == 05 | `yy' == 06 | `yy' == 07  {
			local i = "20"+"`yy'"
			import excel using "http://www.wiod.org/protected3/data13/wiot_analytic/wiot`yy'_row_apr12.xlsx",clear
		}
		
		if `yy' == 08 | `yy' == 09 | `yy' == 10 | `yy' == 11 {
			local i = "20"+"`yy'"
			import excel using "http://www.wiod.org/protected3/data13/update_sep12/wiot/wiot`yy'_row_sep12.xlsx",clear
		}
		
		* Remove final consumption columns
		rename * c(####)_=, renumber
		drop c0004 c1440-c1644
		rename c(####)_* .*

		* Rename variables
		ds A B C BKG, not 
		foreach var of varlist `r(varlist)' {
			local newvarname = strtoname(`var'[3]+"_"+`var'[5])
			rename `var' INT_`newvarname'
			}
		rename *__* *_*
		rename A CODE
		rename B DESC
		rename C COUNTRY
		rename BKG TOT
		drop in 1/6
		destring INT_* TOT,replace
		rename *, lower	
	}
	

	replace code="VAL" if desc=="Value added at basic prices"
	replace code="TOT" if country=="TOT"
	replace country="TOT_VAL" if desc=="Value added at basic prices"
	drop if country==""

	*in 2010, country code for Romania changed from ROM to ROU
	capture replace country="ROM" if country=="ROU"
	capture rename int_*_rou int_*_rom

	gen panel = code+country
	
	if $WEB == 0 {
		drop v*
		destring int*, replace force
		drop fin*	
	}
	
	compress
	save temp.dta, replace

	*** Reshape to create panel dataset for intermediate use
	drop tot
	drop if code=="TOT"
	drop if code=="VAL"

	reshape long int_, i(panel) j(country_ind) string
	rename panel partner_ind
	drop code desc country
	gen partner = substr(partner_ind, -3,.)
	gen country  = substr(country_ind, -3,.)

	replace partner_ind  = substr(partner_ind,1,length(partner_ind)-3)
	replace country_ind = substr(country_ind,1,length(country_ind)-4)
	replace partner_ind=strlower(partner_ind)
	replace country_ind=strlower(country_ind)
	replace country=strupper(country)
	replace partner=strupper(partner)

	gen year=`i'
	sort country partner country_ind partner_ind
	compress
	save WIOD_`i'.dta, replace

	*** Merge with total output by industry
	use temp, clear
	keep code country tot
	drop if code=="TOT"
	drop if code=="VAL"

	rename code country_ind
	replace country=strupper(country)
	replace country_ind=strlower(country_ind)
	compress
	save temp1, replace

	use WIOD_`i'.dta, clear
	merge m:1 country country_ind using temp1
	sort country partner country_ind partner_ind
	drop _merge
	compress
	save WIOD_`i'.dta, replace

	*** Merge with total intermediate consumption
	use temp, clear
	keep if country=="TOT"
	reshape long int_, i(panel) j(country_ind) string
	keep country_ind int_
	gen country  = substr(country_ind, -3,.)
	replace country_ind = substr(country_ind,1,length(country_ind)-4)
	replace country_ind=strlower(country_ind)
	replace country=strupper(country)

	rename int_ tot_inter_cons
	compress
	save temp1.dta, replace

	use WIOD_`i'.dta, clear
	merge m:1 country country_ind using temp1
	sort country partner country_ind partner_ind
	drop _merge
	compress
	save WIOD_`i'.dta, replace

	*** Merge with total value added
	use temp, clear
	keep if country=="TOT_VAL"
	reshape long int_, i(panel) j(country_ind) string
	keep country_ind int_
	gen country  = substr(country_ind, -3,.)
	replace country_ind = substr(country_ind,1,length(country_ind)-4)
	replace country_ind=strlower(country_ind)
	replace country=strupper(country)

	rename int_ tot_val_add
	compress
	save temp1.dta, replace

	use WIOD_`i'.dta, clear
	merge m:1 country country_ind using temp1
	sort country partner country_ind partner_ind
	drop _merge
	compress
	save WIOD_`i'.dta, replace

}
********************************************************************************

********************************************************************************
***APPEND ALL YEARS INTO ONE FILE
********************************************************************************

use WIOD_1995.dta, clear
forvalues i=1996/2011 {
	append using WIOD_`i'.dta
}
sort country partner country_ind partner_ind year
compress
save WIOD.dta, replace

********************************************************************************
***RENAME AND LABEL
********************************************************************************
	
use WIOD.dta, clear
rename int_ inter_use
rename tot total_output

label var year "Year"
label var country "Reporting country"
label var partner "Partner country"
label var country_ind "Reporting country indutry, WIOD NACE aggregation"
label var partner_ind "Partner country indutry, WIOD NACE aggregation"
label var inter_use "Intermediate use of goods, MIO USD, current prices"
label var total_output "Total outbut by WIOD industry, MIO USD, current prices"
label var tot_inter_cons "Total intermediate consumption by WIOD industry, MIO USD, current prices"
label var tot_val_add "Total value added by WIOD industry, MIO USD, current prices"

sort country country_ind year partner partner_ind
order country country_ind partner partner_ind year
compress
save WIOD.dta, replace

* Next 
cd "$rootfolder\data"
